Skip to content

feat(query): add restore and restorePages to undo drop - #342

Merged
dangreen merged 1 commit into
mainfrom
feat/query-restore
Nov 6, 2026
Merged

dangreen merged 1 commit into
mainfrom
feat/query-restore

Conversation

@dangreen

@dangreen dangreen commented Nov 6, 2026

Copy link
Copy Markdown
Member

Why

$data(key, value, undo) lets a write take back only its own change, but the function that undoes a removal from a list had to be written by hand in every app: find the item in the previous data, put it back where it stood, and leave the list alone if a refetch has already brought it back. A plain revert does not fit here: it brings the whole list back and drops what was written since, such as an item added while the removal was in flight.

What

  • restore(array, prev, what) puts values removed with drop back at their previous indexes in the current array. It takes back every match, since drop removes them all, and leaves an array that holds a matching value again as it is, so nothing comes back twice.
  • restorePages(pages, prev, what, callback) does the same for infinite pages, each page from the page with the same number in prev. The callback adapts the page shape, as in sortPages. Pages stay as they are when a matching item is back on any page, and a page that is no longer loaded gets nothing back.
  • Both take the data as $data passes it to undo, null included.
  • Docs: the helpers in Cache Mutation Helpers, with a note to wrap the result in sort or sortPages when the client knows the order of the list, an example next to undo in Reading and Writing Data, and a point in the nano-kit-query skill, with its DOCS.md regenerated.

Size

Entry Before After Limit
All publics (Gzip) 4600 B 4717 B 4.6 → 4.75 kB
All publics (Brotli) 4242 B 4346 B 4.25 → 4.35 kB
Minimal set (Gzip) 1868 B 1868 B 1.9 kB
Minimal set (Brotli) 1748 B 1748 B 1.75 kB

Only code that imports the helpers pays for them: restore adds 52 B on top of drop, restorePages adds 125 B on top of mapPages and drop.

An item goes back to its previous index, so one added in the meantime can shift it by a place. Matching neighbours with an id selector closes that, but makes restore two and a half times heavier, and a list with a known order gets the exact place from sort and sortPages anyway.

Tests

utils.spec.ts: one value and several values put back, what was written since, an array that became shorter or empty, no mutation of the input, a value that is back already, empty values, and for pages the page an item stood in, the pagination of the current pages, items of several pages, a custom page shape, an item back on another page, a page loaded since and a page no longer loaded. cache.spec.ts: both helpers as the undo of a real $data write. oxlint, tsc and vitest are clean in @nano_kit/query (214 tests), size-limit passes, the pre-push pnpm test passes across the monorepo and the website builds.

A plain revert brings the whole list back and drops what was written since. They put removed items back into the data as it is by now, for the third argument of `$data`.
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.95%. Comparing base (eb754ab) to head (43e3db5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #342      +/-   ##
==========================================
+ Coverage   83.92%   83.95%   +0.02%     
==========================================
  Files         231      231              
  Lines        4958     4967       +9     
  Branches     1087     1090       +3     
==========================================
+ Hits         4161     4170       +9     
  Misses        560      560              
  Partials      237      237              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dangreen
dangreen merged commit 22be0e7 into main Nov 6, 2026
11 checks passed
@dangreen
dangreen deleted the feat/query-restore branch November 6, 2026 16:07
@github-actions github-actions Bot mentioned this pull request Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant